package com.zzdx.deployment.web.controller.company;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import com.zzdx.deployment.framework.util.ShiroUtils;
import com.zzdx.deployment.system.domain.CompanyAndPc;
import com.zzdx.deployment.system.domain.SysUser;
import com.zzdx.deployment.system.mapper.CompanyMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zzdx.deployment.common.annotation.Log;
import com.zzdx.deployment.common.enums.BusinessType;
import com.zzdx.deployment.system.domain.Company;
import com.zzdx.deployment.system.service.ICompanyService;
import com.zzdx.deployment.framework.web.base.BaseController;
import com.zzdx.deployment.common.page.TableDataInfo;
import com.zzdx.deployment.common.base.AjaxResult;
import com.zzdx.deployment.common.utils.ExcelUtil;

/**
 * 企业 信息操作处理
 * 
 * @author zzdx
 * @date 2020-03-08
 */
@Controller
@RequestMapping("/system/company")
public class CompanyController extends BaseController
{

    private String prefix = "system/company";
	
	@Autowired
private ICompanyService companyService;
	
	@RequiresPermissions("system:company:view")
	@GetMapping()
	public String company()
	{
	    return prefix + "/company";
	}
	
	/**
	 * 查询企业列表
	 */
	@RequiresPermissions("system:company:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Company company)
	{
		startPage();
        List<Company> list = companyService.selectCompanyList(company);
		return getDataTable(list);
	}

    @Autowired
	private CompanyMapper companyMapper;

	@PostMapping("/listSelf")
	@ResponseBody
	public Company listSelf()
	{
		startPage();
		SysUser sysUser = ShiroUtils.getSysUser();
		Integer userId = Integer.valueOf(String.valueOf(sysUser.getUserId()));
		Company company1 = companyMapper.selectCompanyByUSerId(userId);
		return company1;
	}
	/**
	 * 查询企业列表
	 */
	@RequiresPermissions("system:company:companyAndPcList")
	@PostMapping("/companyAndPcList")
	@ResponseBody
	public TableDataInfo getCompanyAndPclist(CompanyAndPc companyAndPc)
	{
		startPage();
		List<CompanyAndPc> list = companyService.selectCompanyAndPcList(companyAndPc);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出企业列表
	 */
	@RequiresPermissions("system:company:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Company company)
    {
    	List<Company> list = companyService.selectCompanyList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        return util.exportExcel(list, "company");
    }
	
	/**
	 * 新增企业
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		SysUser user = ShiroUtils.getSysUser();
		Company company= companyMapper.selectCompanyByUSerId(Integer.parseInt(user.getUserId().toString()));
		if(company==null){
			mmap.put("companyPhone",user.getPhonenumber());
			return prefix + "/add";
		}else{
			mmap.put("company",company);

			return "/system/pc/pc";
		}

	}


	/**
	 * 新增保存企业
	 */
	@RequiresPermissions("system:company:add")
	@Log(title = "企业", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Company company)
	{
		SysUser user = ShiroUtils.getSysUser();
		Company c= companyMapper.selectCompanyByUSerId(Integer.parseInt(user.getUserId().toString()));
		if(c!=null){
			return error("您已创建企业请不要重复创建");
		}
		long userId= user.getUserId();
        company.setUserId(Integer.valueOf(String.valueOf(userId)));
		return toAjax(companyService.insertCompany(company));
	}

	/**
	 * 修改企业
	 */
	@GetMapping("/edit/{companyId}")
	public String edit(@PathVariable("companyId") Integer companyId, ModelMap mmap)
	{
		Company company = companyService.selectCompanyById(companyId);
		mmap.put("company", company);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存企业
	 */
	@RequiresPermissions("system:company:edit")
	@Log(title = "企业", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Company company)
	{		
		return toAjax(companyService.updateCompany(company));
	}
	
	/**
	 * 删除企业
	 */
	@RequiresPermissions("system:company:remove")
	@Log(title = "企业", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(companyService.deleteCompanyByIds(ids));
	}
	
}
